//
// Copyright (c) 2003-2005 BroadVision, Inc. All rights reserved.
//
// This software is copyrighted.  Under the copyright laws, this software
// may not be copied, in whole or in part, without prior written consent
// of BroadVision, Inc. or its assignees.  This software is provided under
// the terms of a license between BroadVision and the recipient, and its
// use is subject to the terms of that license.
//
// This software may be protected by one or more U.S. and International
// patents.  Certain applications of BroadVision One-To-One software are
// covered by U.S. patent 5,710,887.
//
// TRADEMARKS: BroadVision and BroadVision One-To-One are registered
// trademarks of BroadVision, Inc., in the United States and the European
// Community, and are trademarks of BroadVision, Inc., in other
// countries.  The BroadVision logo, is a trademark of BroadVision, Inc.,
// in the United States and other countries. Additionally, IONA and Orbix
// are trademarks of IONA Technologies, Ltd.  RSA, MD5, and RC2 are
// trademarks of RSA Data Security, Inc.
//

package com.broadvision.manage.common.tools.action.inbox;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.Globals;
import com.broadvision.visitor.client.Visitor;
import com.broadvision.data.common.TableData;
import com.broadvision.data.common.RowData; 
import com.broadvision.data.common.DataValue;
import com.broadvision.alerts.client.AlertManager;
import com.broadvision.web.servlet.SessionVisitor;
import com.broadvision.web.util.ViewDynaBean;
import com.broadvision.manage.tools.action.BaseAction;
import com.broadvision.manage.common.tools.action.user.UserUtils;

/**
 * Display the visitor's assigned tasks.
 */
public class InboxAction extends BaseAction {

  public static final String VIEW_BEAN_NAME = "mtInboxView";

  public static final String DTO_BEAN_NAME = "mtInboxDTO";

  private static final String[] alertCategories =
    {
          "Process",
          "Collaboration Space Task",
          "Collaboration Space Meeting",
          "Collaboration Space Participant",
          "Delegation",
          "Workflow Task Event",
          "Notification Step",
          "AA Generated",
          "Job Driven",
          "Other"
    };

  /**
   * Implements the action for displaying the visitor's assigned tasks.
   * <p> <p>
   * @param mapping <code>ActionMapping</code> object used to select this instance
   * @param form Optional <code>ActionForm</code> bean for this request (if any)
   * @param request <code>HttpServletRequest</code> object to process
   * @param response <code>HttpServletResponse</code> object to process
   * <p> <p>
   * @return <code>ActionForward</code> instance describing where and how
   * control should be forwarded, or null if the response has already been completed
   * <p> <p>
   * @throws Exception if an error occurs
   */
  public ActionForward processExecute(ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)
                               throws Exception {
    if (request.getAttribute(Globals.ERROR_KEY) == null) {
      //No error message. This means it's not from validation error page.
      prepareFormBean(form, request);
    }

    //set data into view bean.
    prepareViewBeans(mapping, form, request);

    return mapping.findForward("success");
  }
 
  /**
   *
   * <p> <p>
   * @param form <code>ActionForm</code>
   * @param request <code>HttpServletRequest</code>
   */
  protected void prepareFormBean(ActionForm form,
                                 HttpServletRequest request)
                          throws Exception {
    Visitor visitor = getVisitor(request);

    PropertyUtils.setSimpleProperty(form, "userId", new Long(visitor.getId()));
  }

  /**
   *
   * <p> <p>
   * @param mapping <code>ActionMapping</code>
   * @param form <code>ActionForm</code>
   * @param request <code>HttpServletRequest</code>
   */
  protected void prepareViewBeans(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request)
                           throws Exception {
    Visitor visitor = getVisitor(request);

    ViewDynaBean view = new ViewDynaBean(VIEW_BEAN_NAME, mapping);
    request.setAttribute(VIEW_BEAN_NAME, view);

    String formattedName =
      UserUtils.getFormattedName(visitor.getId(),
				 getResources(request, "mt.messages"),
				 getLocale(request));

    UserUtils.setP(view, "fullName", formattedName);

    AlertManager alertMgr = new AlertManager();

    String[] columns = new String[8];
    columns[0] = "ALERT_NAME";
    columns[1] = "MESSAGE_ID";
    columns[2] = "CREATION_DATE";
    columns[3] = "DATE_READ";
    columns[4] = "FROM_WHOM";
    columns[5] = "SUBJECT";
    columns[6] = "ALERT_MESSAGE";
    columns[7] = "JOB_ID";

    TableData messages = 
      alertMgr.getInboxMessages(visitor.getId(), columns, alertCategories, 0, 0);
    int numMessages = (messages == null) ? 0 : messages.size();

    List dtos = new ArrayList(numMessages);
    PropertyUtils.setSimpleProperty(view, "alerts", dtos);

    for (int rowIndex = 0; rowIndex < numMessages; rowIndex++) {
      RowData row = messages.getRow(rowIndex);

      ViewDynaBean dto = new ViewDynaBean(DTO_BEAN_NAME, mapping);

      DataValue value = row.getValue(columns[0]);
      PropertyUtils.setSimpleProperty(dto, "alertName", value.getString());
      value = row.getValue(columns[1]);
      PropertyUtils.setSimpleProperty(dto, "messageId", value.getString());
      value = row.getValue(columns[2]);
      PropertyUtils.setSimpleProperty(dto, "creationDate", value.getDateTime());
      value = row.getValue(columns[3]);
      PropertyUtils.setSimpleProperty(dto, "dateRead", value.getDateTime());
      value = row.getValue(columns[4]);
      PropertyUtils.setSimpleProperty(dto, "from", value.getString());
      value = row.getValue(columns[5]);
      PropertyUtils.setSimpleProperty(dto, "subject", value.getString());
      value = row.getValue(columns[6]);
      PropertyUtils.setSimpleProperty(dto, "alertMessage", value.getString());
      value = row.getValue(columns[7]);
      PropertyUtils.setSimpleProperty(dto, "jobId", value.getString());

      dtos.add(dto);
    }
  }
}
